<?php
require_once('wmad.class.php');
require_once("log.change.class.php");
/*
 *	DB funkcije
 *	Sve potrebne funkcije za insert update i delete.
 *
 *
*/
class db_functions{
	// Attributes 
	var $_site_config;//objekat koji ima metodu getDB() koja vraca pear db objekat
	//var $numrows;
	//Konstruktor
	function db_functions($encoding="utf8"){
		$this->_site_config = new wmad();
		$q = "SET NAMES $encoding";
		$this->alterTable($q);
    }
    // Metode
    /**
    *    XXX
    *    Dovlaci jedinstven id broj
    *    @access private 
    */
    function getId ($idname="uniq"){
    	$config = $this->_site_config;
    	$conn = $config->getDb();
    	$result = $conn->nextId($idname);
    	if (DB::isError ($result)) {
    		return FALSE;
    	}
    	return $result;
    }
    /**
    *    XXX
    *    Dovlaci jednu informaciju
    *    @access private 
    */
    function getOne ($sql){
    	$config = $this->_site_config;
    	$conn = $config->getDb();
    	$result = $conn->getOne($sql);
    	if (DB::isError ($result)) {
    		return FALSE;
    	}
    	
    	return $result;
    }
    /**
    *    XXX
    *    Dovlaci jedan (prvi) red
    *    @access private 
    */
    function getRow ($sql,$assoc=false){
    	$config = $this->_site_config;
    	$conn = $config->getDb();
    	if ($assoc) {
    		$res = $conn->query($sql);
    		$result = $res->fetchRow(DB_FETCHMODE_ASSOC);
    	}else{
    		$result = $conn->getRow($sql);
    	}
    	if (DB::isError ($result)) {
    		return FALSE;
    	}
    	return $result;
    }
    /**
    *    XXX
    *    Ova metoda vraca jednu kolonu rezultata u array-u
    *    @access private 
    */
    function getCol ($sql){
    	$config = $this->_site_config;
    	$conn = $config->getDb();
    	$result = $conn->getCol($sql);
    	if (DB::isError ($result)) {
    		return FALSE;
    	}
    	if (is_array($result)) {
    		return $result;
    	}else{
    		return FALSE;
    	}
    }
    /**
    *    XXX
    *    Ova metoda vrsi select i vraca asocijativni array
    *    @access private 
    */
    function selectAssoc ($sql){
    	$config = $this->_site_config;
    	$conn = $config->getDb();
    	$result = $conn->query($sql);
			if (DB::isError ($result)) {
    			trigger_error ("Query nije uspeo: ".$result->getMessage ()." \n<br />".$result->getDebugInfo(), E_USER_WARNING);
    			return FALSE;
    		}
    	while ($row = $result->fetchRow()) {
    		$vrati[$row[0]] = $row[1];
    	}
    	if (!is_array($vrati)) {
    		return FALSE;
    	}else{
    		return $vrati;
    	}
    }
    /**
    *    XXX
    *    Ova metoda vrsi select i vraca 2d array
    *    @access private 
    */
    function select1 ($sql) {
    	$config = $this->_site_config;
    	$conn = $config->getDb();
    	$result = $conn->query($sql);
			if (DB::isError ($result)) {
    			trigger_error ("Query nije uspeo: ".$result->getMessage ()." \n<br />".$result->getDebugInfo(), E_USER_WARNING);
    			return FALSE;
    		}
    	//file_put_contents("tmpVOJA",var_export($result,true));
    		$this->numrows = $result->numRows();
    	while ($row = $result->fetchRow()) {
    		$vrati[] = $row;
    	}
    	if (!is_array($vrati)) {
    		return FALSE;
    	}else{
    		return $vrati;
    	}
    }
    /**
    *    XXX
    *    Ova metoda vrsi select i vraca assoc 2d array
    *    @access private 
    */
    function select2 ($sql) {
    	$config = $this->_site_config;
    	$conn = $config->getDb();
    	$result = $conn->query($sql);
			if (DB::isError ($result)) {
    			trigger_error ("Query nije uspeo: ".$result->getMessage ()." \n<br />".$result->getDebugInfo(), E_USER_WARNING);
    			return FALSE;
    		}
    	if (method_exists($result,"numRows")) {
    		$this->numrows = $result->numRows();
    	}else{
    		$this->numrows = null;
    	}
    	
    	while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC)) {
    		$vrati[] = $row;
    	}
    	if (!is_array($vrati)) {
    		return FALSE;
    	}else{
    		return $vrati;
    	}
    }
    /**
    *    XXX
    *    Ova metoda vrsi update, insert ili delete
    *    @access private 
    */
    function alterTable ($sql){
    	$config = $this->_site_config;
    	$conn = $config->getDb();
    	$result = $conn->query($sql);
    	if (DB::isError ($result)) {
			if (stristr($result->getMessage(), "already exists")) {
				return FALSE;
			}
			trigger_error ("Query nije uspeo: ".$result->getMessage ()." \n<br />".$result->getDebugInfo(), E_USER_WARNING);
			return FALSE;
		}
		
    	return TRUE;
    }
    /**
    *    XXX
    *    Ova metoda vraca samo broj redova na koje je uticao query
    *    @access private 
    */
    function numRows ($sql) {
    	$config = $this->_site_config;
    	$conn = $config->getDb();
    	$result = $conn->query($sql);
    	if (DB::isError ($result)) {
    			trigger_error ("Query nije uspeo: ".$result->getMessage ()." \n<br />".$result->getDebugInfo(), E_USER_WARNING);
    			return FALSE;
    		}
    	return $conn->affectedRows();
    }
    
    /**
     * XXX
     * Ova metoda kreira privremenu tabelu
     *
     * @param String $name
     * @param Object $params
     * @return bool
     * @access private
     */
    function createTempTable($name,$params){
    	$config = $this->_site_config;
    	$conn = $config->getDb();
    	$q = "CREATE TEMPORARY TABLE IF NOT EXISTS $name (";
    	foreach ($params as $k=>$v){
    		$q .= "$k $v,";
    	}
    	$q = substr($q,0,strlen($q)-1).")";
    	$result = $conn->query($q);
    	if(DB::isError ($result)){
    		trigger_error("Query nije uspeo: ".$result->getMessage()."\n<br />".$result->getDebugInfo(),E_USER_WARNING);
    		return false;
    	}
    	return true;
    }
    
    function errorMessage($code=null){
    	$this->loadErrorMessages();
    	if ($code==null) {
    		$conn = $this->_site_config->_database->connection;
    		$code = mysql_errno($conn);
    	}
    	return $this->_errorMessages[$code]["message"];
    }
    function loadErrorMessages(){
    	$filename = "classes/mysql_error_codes.txt";
    	$file = file($filename);
    	$keys = array_splice($file,0,1);
    	foreach ($file as $line){
    		$cell = explode("\t",$line);
    		$c	= trim($cell[0]);
    		$sql= trim($cell[1]);
    		$msg= trim($cell[2]);
    		$this->_errorMessages[$c]["sql_statement"] = $sql;
    		$this->_errorMessages[$c]["message"] = $msg;
    	}
    }
    function getDBName(){
    	return $this->_site_config->_database->dsn["database"];
    }
}
?>